package com.jd.after.modules.mis.service.impl;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jd.after.modules.mis.dao.BlogDao;
import com.jd.after.modules.mis.entity.Blog;
import com.jd.after.modules.mis.entity.Blogimg;
import com.jd.after.modules.mis.service.BlogService;
import com.jd.after.modules.mis.service.BlogimgService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Service==》自动注解==》spring管理==》controller @autowried 注入
 * 
 *                                         业务方法，事务 ServiceImpl<BlogDao, Blog>
 * 
 * 
 */
@Service
public class BlogServiceImpl extends ServiceImpl<BlogDao, Blog> implements BlogService {


    @Autowired
    BlogimgService blogimgService;



    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveBlogAndImg(Blog entity) {

        //1.保存博客，返回id
        this.save(entity);

        //2.保存博客多图片
        for (Blogimg b : entity.getBlogmulimglist()) {
            b.setBlogId(entity.getId());
        }

        blogimgService.saveBatch(entity.getBlogmulimglist());
        

    }


     /**
    * 更新博客和多图片   ，预留
    */
   public void updateBlogAndImg(Blog entity){

   }



   /**
      * 删除博客和多图片
      */
      @Transactional(rollbackFor = Exception.class)
     public void deleteBlogAndImg(Long[] ids){
         this.removeByIds(Arrays.asList(ids));

         for(Long id : ids){
            blogimgService.remove(new QueryWrapper<Blogimg>().eq("blog_id",id));
         }
         
     }




    @Override
    public List<Map<String, Object>> tjBlogNum(Map<String, Object> map) {
        // this.baseMapper=== BlogDao

        List<Map<String, Object>> result = this.baseMapper.tjBlogNum(map);
        return result;
    }

}
